<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="90px">
      <el-form-item label="资金账户" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入资金账户"
          maxlength="30"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="银行开户行" prop="accountBank">
        <el-input
          v-model="queryParams.accountBank"
          placeholder="请输入银行开户行"
          maxlength="30"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="银行卡号" prop="bankCard">
        <el-input
          v-model="queryParams.bankCard"
          placeholder="请输入银行卡号"
          maxlength="30" @input="v => queryParams.bankCard = v.replace(/[^\d]/g,'')"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:account:add']"
        >新增</el-button>
      </el-col>

      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:account:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:account:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="accountList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" type="index" width="50" align="center">
        <template slot-scope="scope">
          <span>{{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}</span>
        </template>
      </el-table-column>
      <el-table-column label="资金账户" align="center" prop="name" />
      <el-table-column label="银行开户行" align="center" prop="accountBank" />
      <el-table-column label="银行卡号" align="center" prop="bankCard" />
      <el-table-column label="开户时间" align="center" prop="openingTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.openingTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注信息" align="center" prop="remarks" show-overflow-tooltip="true" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:account:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:account:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改资金账户管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" :close-on-click-modal = "false" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="资金账户" prop="name">
          <el-input v-model="form.name" placeholder="请输入资金账户" maxlength="30"/>
        </el-form-item>
        <el-form-item label="银行开户行" prop="accountBank">
          <el-input v-model="form.accountBank" placeholder="请输入银行开户行" maxlength="30"/>
        </el-form-item>
        <el-form-item label="银行卡号" prop="bankCard">
          <el-input v-model="form.bankCard" placeholder="请输入银行卡号" maxlength="30" @input="v => form.bankCard = v.replace(/[^\d]/g,'')"/>
        </el-form-item>
        <el-form-item label="开户时间" prop="openingTime">
          <el-date-picker clearable
            v-model="form.openingTime"
            type="date"
            value-format="yyyy-MM-dd"
            style="width: 100%"
            placeholder="请选择开户时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="备注信息" prop="remarks">
          <el-input v-model="form.remarks" type="textarea" placeholder="请输入备注信息" maxlength="200"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listAccount, getAccount, delAccount, addAccount, updateAccount } from "@/api/erp/account";

export default {
  name: "Account",
  data() {
    return {
      // 遮罩层
      loading: true,
      //期初余额只读状态
      startBalanceType: false,
      // 选中数组
      ids: [],
      // 选中数组names
      names: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 资金账户管理表格数据
      accountList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        accountBank: null,
        bankCard: null,
        startBalance: null,
        capitalBalance: null,
        openingTime: null,
        remarks: null,
        createUser: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [
          { message: "资金账户不能为空", trigger: "blur", required: true}
        ],
        accountBank: [
          { message: "开户行不能为空", trigger: "blur", required: true}
        ],
        bankCard: [
          { message: "银行卡号不能为空", trigger: "blur", required: true}
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询资金账户管理列表 */
    getList() {
      this.loading = true;
      listAccount(this.queryParams).then(response => {
        this.accountList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        accountBank: null,
        bankCard: null,
        startBalance: null,
        capitalBalance: null,
        openingTime: null,
        remarks: null,
        createTime: null,
        updateTime: null,
        createUser: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.names = selection.map(item => item.name)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      //期初余额可以输入
      this.startBalanceType= false
      this.reset();
      this.open = true;
      this.title = "添加资金账户管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      //期初余额变只读
      this.startBalanceType= true
      this.reset();
      const id = row.id || this.ids
      getAccount(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改资金账户";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateAccount(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addAccount(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      const names = row.name || this.names;
      this.$modal.confirm('是否确认删除资金账户"' + names + '"的数据项？').then(function() {
        return delAccount(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/account/export', {
        ...this.queryParams
      }, `account_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
